package org.zjt.gc;

import lombok.Builder;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.zjt.jsr.aa.Getter;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-06-01 下午3:11
 * @see
 */



public class InnerLostTest{


    public static void main(String[] args) {




        ReferenceQueue<UserAction> rq2 = new ReferenceQueue<>();
        UserAction userAction = new UserAction(User2.builder().name("sss").age(23).build());
        PhantomReference<UserAction>  userActionPhantomReference = new PhantomReference(userAction,rq2);

        System.out.println(userActionPhantomReference.get());




        ReferenceQueue<Runnable> rq = new ReferenceQueue<>();
        Runnable runnable = () -> userAction.action();
        PhantomReference<Runnable> runnablePhantomReference = new PhantomReference<>(runnable,rq);

        System.gc();
        Runnable runnable1 = runnablePhantomReference.get();
        System.out.println(runnable1);

        runnable.run();

    }
}


@Slf4j
class UserAction {

    private final User2 user ;

    public UserAction(User2 user) {
        this.user = user;
    }


    public void action() {
        log.info(String.valueOf(user));
    }

    @Override
    protected void finalize() throws Throwable {
        System.err.println("---------finalize-------------");
    }

}

@Builder
@Getter
@ToString
class User2{
    private String name;
    private Integer age;
}